package com.wlcj.www.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.nutz.ioc.loader.annotation.IocBean;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.wlcj.constant.Global;
import com.wlcj.www.model.main.Channel;
import com.wlcj.www.model.main.ChannelExt;
import com.wlcj.www.model.main.Content;
import com.wlcj.www.model.main.ContentExt;

@IocBean
public class IndexService {
	
	public List<Channel> getTopChannels() {
		List<Channel> list = Channel.dao.getTopChannels();
		
		for(Channel c:list) {
			String innerUrl = Global.WWW_DOMAIN+c.getStr("channel_path")+"/index.jhtml";
			ChannelExt ce = ChannelExt.dao.findById(c.getInt("channel_id"));
			c.put("name", ce.getStr("channel_name"));
			String link = ce.getStr("link");
			c.put("url", StringUtils.isBlank(link)?innerUrl:link);
		}
		return list;
	}
	
	public Record getChannelAd(int channelId) {
		Content content = Content.dao.findById(channelId);
		Record r = new Record();
		int contentId = content.getInt("content_id");
		ContentExt ce = ContentExt.dao.findById(contentId);
		r.set("img", ce.getStr("title_img"));
		r.set("url", ce.getStr("origin_url"));
		return r;
	}
	
	public String getLoginNameById(int uid) {
		String sql = "select realname from jc_user_ext where user_id = ?";
		Record record = Db.findFirst(sql, uid);
		String name = record.getStr("realname");
		if(StringUtils.isBlank(name)) {
			sql = "select username from jc_user where user_id = ?";
			record = Db.findFirst(sql, uid);
			name = record.getStr("username");
		}
		return name;
	}

}
